Skip to content

Type inference: Add support for named arguments - #22973

Open
hvitved wants to merge 4 commits into
github:mainfrom
hvitved:type-inference-named-args
Open

hvitved wants to merge 4 commits into
github:mainfrom
hvitved:type-inference-named-args

Conversation

@hvitved

@hvitved hvitved commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

This PR aligns the interface for parameters and arguments of the type inference library with that of Unified, in order to be able to handle arguments passed in by name. While Swift requires named arguments to respect the parameter declaration order, this is not assumed in the type inference library, which means that the implementation should work for e.g. C# as well.

Swift allows for multiple parameters with the same external name, so we account for that by tagging named arguments/parameters with their rank. However, the following corner-case is not handled:

func f(a first: (Int) -> Void, a second: (String) -> Void) {}

f { x in
} a: { y in
}

Since the first trailing closure is not labeled, it gets matched (correctly) by position, but the second trailing closure is considered the first a argument, so it is also matched against the first parameter instead of the second.

This PR also changes the inferInvocationArgumentTypeContextualDefault interface in order to handle contextual propagation into synthetic arguments (documented with a test case).

@github-actions github-actions Bot added Rust Pull requests that update Rust code Unified labels Nov 2, 2026
@hvitved
hvitved force-pushed the type-inference-named-args branch from baaa8f7 to e6d8f96 Compare November 11, 2026 10:15
@hvitved
hvitved requested a max review from Copilot November 11, 2026 10:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Argument matching mishandles repeated Swift labels and labelled enum-case parameters, producing spurious return types and breaking generic type propagation.

2 open issues
What changed in this PR

Adds named-argument matching to shared type inference, enabling Swift to infer argument types when earlier defaulted parameters are omitted.

Changes:

  • Exposes argument names and parameter labels through the inference interfaces.
  • Adds label-based matching while retaining positional arguments for Rust.
  • Updates Swift test annotations and generated inference expectations.
File Description
unified/​ql/​test/​library-tests/​type-inference/​type-inference.expected Records inferred argument-node types and improved closure typing.
unified/​ql/​test/​library-tests/​type-inference/​constructors.swift Updates an inline expectation comment for closure parameter inference.
unified/​ql/​lib/​codeql/​unified/​internal/​typeinference/​TypeInference.qll Introduces named argument wrappers and receiver handling.
unified/​ql/​lib/​codeql/​unified/​internal/​ParameterEx.qll Exposes external names for source and synthesized parameters.
shared/​typeinference/​codeql/​typeinference/​internal/​TypeInference.qll Matches arguments to parameters by name or position.
rust/​ql/​lib/​codeql/​rust/​internal/​typeinference/​TypeInference.qll Adapts positional-only Rust arguments to the shared interface.

Review effort: Max

@hvitved
hvitved force-pushed the type-inference-named-args branch from e6d8f96 to d6d329e Compare November 11, 2026 21:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved type-inference defects cause label/rank key collisions, misalign repeated-label trailing closures, and block expected-type propagation to function-value call arguments.

3 open issues
2 resolved since last review

Review effort: Max

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Trailing-closure matching regressions reuse already-consumed parameters for repeated labels and mishandle _ labels, producing incorrect or missing inferred types.

2 open issues
3 resolved since last review

Review effort: Max

@hvitved
hvitved force-pushed the type-inference-named-args branch 6 times, most recently from 48791ee to dc74749 Compare November 16, 2026 13:36
@hvitved
hvitved force-pushed the type-inference-named-args branch from dc74749 to 6dff9e0 Compare November 16, 2026 13:44
@hvitved hvitved added the no-change-note-required This PR does not need a change note label Nov 16, 2026
@hvitved
hvitved marked this pull request as ready for review November 16, 2026 20:11
@hvitved
hvitved requested a review from a team as a code owner November 16, 2026 20:11
@hvitved
hvitved requested review from a team as code owners November 16, 2026 20:11

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-change-note-required This PR does not need a change note Rust Pull requests that update Rust code Unified

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants